package org.netEase.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.netEase.dao.shangpingDao;
import org.netEase.entity.shangping;
import org.netEase.util.BaseDao;

public class shangpingDaoImple extends BaseDao implements shangpingDao {

	@Override
	public List<shangping> listGetByZid(int PageNo, int sizes, int zid) {
		List<shangping> list = new ArrayList<>();
		List<Object> condition = new ArrayList<>();
		String sql = "select * from shangping where 1=1";
		if(zid>0&&zid!=0) {
			sql+=" and Zid = ?";
			condition.add(zid);
		}
		sql+=" limit ?,?";
		int start = (PageNo - 1) * sizes;
		condition.add(start);
		condition.add(sizes);
		ResultSet rs = super.executeQuery(sql, condition.toArray());
		
		try {
			while (rs.next()) {
				shangping shangping = new shangping();
				shangping.setBigimg(rs.getString("bigimg"));
				shangping.setJieshao(rs.getString("jieshao"));
				shangping.setJifen(rs.getInt("jifen"));
				shangping.setShopimage(rs.getString("Simg"));
				shangping.setShopname(rs.getString("Sname"));
				shangping.setSid(rs.getInt("sid"));
				shangping.setSimgss(rs.getString("simgss"));
				shangping.setSjiage(rs.getDouble("sjiage"));
				shangping.setYid(rs.getInt("yid"));
				shangping.setZid(rs.getInt("zid"));
				
				list.add(shangping);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public int getbyzid(int zid) {
		String sql = "select count(*) c from shangping where 1=1";
		List<Object> condition = new ArrayList<>();
		int count = 0;
		if(zid>0&&zid!=0) {
			sql+=" and zid=?";
			condition.add(zid);
		}
		ResultSet rs = super.executeQuery(sql, condition.toArray());
		try {
			if(rs.next()) {
				count = rs.getInt("c");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count;
	}
	/*public static void main(String[] args) {
		shangpingDao shangpingDao = new shangpingDaoImple();
		List<shangping> list=shangpingDao.listGetByZid(1, 10000, 20);
		int a = 0;
		String b = "5";
		
		for (shangping shangping : list) {
			System.out.println(shangping);
		}
		
	}*/

	@Override
	public List<shangping> listGetByZid(int PageNo, int sizes, int zid, int yid) {
		List<shangping> list = new ArrayList<>();
		List<Object> condition = new ArrayList<>();
		String sql = "select * from shangping where 1=1";
		if(zid>0&&zid!=0) {
			sql+=" and Zid = ?";
			condition.add(zid);
		}
		if(yid>0&&yid!=0) {
			sql+=" and Yid = ?";
			condition.add(yid);
		}
		sql+=" limit ?,?";
		int start = (PageNo - 1) * sizes;
		condition.add(start);
		condition.add(sizes);
		ResultSet rs = super.executeQuery(sql, condition.toArray());
		try {
			while (rs.next()) {
				shangping shangping = new shangping();
				shangping.setBigimg(rs.getString("bigimg"));
				shangping.setJieshao(rs.getString("jieshao"));
				shangping.setJifen(rs.getInt("jifen"));
				shangping.setShopimage(rs.getString("Simg"));
				shangping.setShopname(rs.getString("Sname"));
				shangping.setSid(rs.getInt("sid"));
				shangping.setSimgss(rs.getString("simgss"));
				shangping.setSjiage(rs.getDouble("sjiage"));
				shangping.setYid(rs.getInt("yid"));
				shangping.setZid(rs.getInt("zid"));
				shangping.setSimg(rs.getString("simg"));
				shangping.setSname(rs.getString("sname"));
				
				list.add(shangping);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public int getCount(int zid,int yid) {
		
			String sql = "select count(1) c from shangping where 1=1";
			List<Object> condition = new ArrayList<>();
			if(zid>0&&zid!=0) {
				sql+=" and zid=?";
				condition.add(zid);
			}
			if(yid>0&&yid!=0) {
				sql+=" and yid=?";
				condition.add(yid);
			}
			ResultSet rs = super.executeQuery(sql,condition.toArray());
			int total = 0;
			try {
				if (rs.next()) {
					total = rs.getInt("c");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					super.closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			return total;
		}

	
	}


